<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    protected $model = null;
    public function __construct(){
        parent::__construct();
        $this->model = D('b_c_wzbm');
    }
    //首页
    public function index(){
        $userid = cookie('userid');
        if(!$userid){
            if(isset($_GET['code'])) {
                $code = $_GET['code'];
                $access_token = componentsgetaccesstoken(); //获取access_token
                $userID = $this->getuserid($code,$access_token); //获取userID

                //判断该微信用户如果没有存入数据库,就获取其信息存入users数据表
                $usersmodel = D('Home/users');
                if(!$usersmodel->where("userid='{$userID}'")->find()){
                     $usersmodel->setuser($access_token,$userID);
                }
                //把当前微信用户的useID存入cookie
                cookie('userid',$userID);
            }
        }
        $this->display();
    }
    /**
     * 获取userID
     * @param $code
     * @param $access_token
     * @return mixed
     */
    public function getuserid($code,$access_token){
        $getuseridURL = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={$access_token}&code={$code}";
        $userid = json_decode(https_request($getuseridURL),true)['UserId'];
        return $userid;
    }

    //详情页
    public function detail(){
        $wzno = I('get.wzno');
        $cpxlno = I('get.cpxlno');
        $cpxlmc = I('get.cpxlmc');
        //如果系列ID不存在说明是从分类跳转过来,从系列表取出系列ID和名称
        if(!$cpxlno){
            $b_p_cpxl = D('Home/b_p_cpxl');
            $xl_info = $b_p_cpxl->getxlinfo($wzno);
            //dump($xl_info);exit;
            $cpxlno = $xl_info[0]['cpxlno'];
            $cpxlmc = $xl_info[0]['xpxlmc'];
        }
        //获取该物资编码的详细信息
        $info = $this->model->xq($wzno);
        //dump($info);exit;

        //获取当前微信的userid
        $userid = cookie('userid');

        //从客服表里判断当前userid如果不是客服,就把userid传到页面
        if( !D('kefu')->where("kefu_user='{$userid}'")->find() ){
            /********************加入浏览历史***********************/
            $historymodel = D('history');
            $historymodel->set_history($userid,$wzno);

            $this->assign(array(
                'userid' => $userid,
            ));

        }

        $this->assign(array(
            'title' => '物资详细信息',
            'info' => $info,
            'cpxlno' => $cpxlno,
            'xlmc' => $cpxlmc,
        ));
        $this->display();
    }

    public function gongn(){
        $this->display();
    }


    public function fankui(){
        $this->display();
    }
    //产品对比页
    public function juxtapose(){
        $wznos = I('get.wzno');
        $info = $this->model->bdwz($wznos);
        //dump($info);exit;
        $this->assign('info',$info);
        $this->display();
    }
    //供应商页面
    public function ghs(){
        $gysno = I('get.gysno');
        $info = $this->model->gysinfo($gysno);
        //dump($info);exit;
        $this->assign('info',$info);
        $this->display();

    }
    //分类列表
    public function cats(){
        $gmodel = D('goods');
        $info = $gmodel->catinfo();
        $this->assign('info',$info);
        $this->display();
    }

    //测试方法
    public function cehua(){
    	$this->display();

    }


    public function signature($accesstoken){
        $ticket = get_components_ticket($accesstoken);
        $noncestr =md5(time());
        $time = time();
        $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        $str1 = "jsapi_ticket={$ticket}&noncestr={$noncestr}&timestamp={$time}&url={$url}";
        $singature = sha1($str1);
        return array(
            'timestamp' => $time,
            'nonceStr' => $noncestr,
            'signature' => $singature,
        );
    }
}